<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/laypage.css">
    <script src="js/vue/vue.min.js"></script>
    <script src="js/jquery/jquery.min.js"></script>
    <script src="js/jquery.validation/1.14.0/jquery.validate.min.js"></script>
    <script src="js/jquery.validation/1.14.0/messages_zh.min.js"></script>
    <script src="js/laypage/laypage.js" charset="utf-8"></script>
    <script src="js/layer/layer.js" charset="utf-8"></script>
    <style>
        input.error {
            border: 1px solid #E6594E;
        }
    </style>
</head>
<body>
<div class="container">

    <form id="addUserForm">
        <br/>
        <div class="form-group">
            <label for="title">标题：</label>
            <input type="text" class="form-control" id="title" name="title" placeholder="输入标题">
        </div>
        <div class="form-group">
            <label for="context">内容：</label>
            <input type="text" class="form-control" id="context" name="context" placeholder="输入内容">
        </div>
        <div class="form-group">
            <label for="pic">图片名：</label>
            <input type="text" class="form-control" id="pic" name="pic" placeholder="输入图片名">
        </div>
        <div class="form-group">
            <button type="button" id="saveBtn" class="btn btn-success">提交</button>
            <button type="button" id="cancelBtn" class="btn btn-default">取消</button>
        </div>
    </form>
</div>
<script>

    var topicAdd = function () {
        if (!check().form()) {
            return;
        }
        $.ajax({
            type: "GET",
            url: "topic_add",
            dataType: "json",
            data: {
                title: $("#title").val(),
                context: $("#context").val(),
                pic: $("#pic").val()
            },
            success: function () {
                $("#cancelBtn").click();
            }
        })
    };

    $("#saveBtn").on('click',function () {
        topicAdd();
    });

    $("#cancelBtn").on('click',function () {
        var index=parent.layer.getFrameIndex(window.name);
        parent.getTopicList();
        parent.layer.close(index);
    });

    function check() {
        //返回一个validate对象,该对象有form方法,返回验证是否通过
        return $("#addUserForm").validate({
            rules: {
                title: {
                    required: true
                },
                context: {
                    required: true
                },
                pic: {
                    required: true
                }
            },
            messages: {
                title: {
                    required: ""
                },
                context: {
                    required: ""
                },
                pic: {
                    required: ""
                }
            }
        });
    };


</script>
</body>
</html>